import Cache from '@/libs/cache';
import Api from '@/libs/api';
import Store from '@/libs/store';

let createdBy = {
    id: 0,
    member_level: 0,
    username: "",
    email: "",
    status: 0,
    created_at: 0,
    updated_at: 0,
    token: "",
    key: "",
    balance: 0,
    frozen: 0,
    cannot_withdrawn: 0,
    signup_message: "",
    nickname: "",
    avatar: "",
    weixin_exceptional_code: "",
    weixin_exceptional_url: "",
    alipay_exceptional_code: "",
    alipay_exceptional_url: ""
}
let resource = {
    id: 0,
    type: 1,
    title: '',
    privacy_status: 2,
    created_at: 0,
    updated_at: 0,
    deleted_at: 0,
    created_by: 0,
    updated_by: 0,
    get_password: "",
    min_level: 0,
    min_integral: 0,
    create_type: 1,
    tieba_url: "",
    tieba_post_ids: "",
    tieba_author_id: "",
    tieba_author_name: "",
    content_total: 1,
    _type: "",
    _enType: "",
    _createdBy: {},
    _canEdit: 0,
    _isFav: 0,
    _isBuy: 0,
    _covers: [
        "http://eim.bnsdmm.top/i/2023/06/19/pr9q9z-2.png"
    ],
    _imageList: [
        {
            id: 0,
            url: "http://eim.bnsdmm.top/i/2023/06/19/pr9q9z-2.png"
        }
    ],
    _contents: [{
        id: 0,
        created_at: 0,
        updated_at: 0,
        deleted_at: 0,
        created_by: 0,
        updated_by: 0,
        resource_id: 0,
        floor_num: 1,
        content: "",
        need_price: "0.00",
        _createdBy: createdBy,
        _content_preview: ""
    }],
    _contentLastUpdatedAt: 0,
    _videos: [
        {
            id: 0,
            created_at: 0,
            updated_at: 0,
            deleted_at: 0,
            created_by: 0,
            updated_by: 0,
            resource_id: 0,
            user_file_id: 0,
            m3u8_relation_path: null,
            m3u8_yii_alias_uploads_path: null,
            m3u8_yii_alias_uploads_abpath: null,
            m3u8_yii_alias_uploads_root: null,
            m3u8_filename: null,
            m3u8_ts_total: 0,
            _createdBy: createdBy,
            _userFile: {
                m3u8_job_status: 0,
                m3u8_screenshots: [],
                _m3u8Aburl: ""
            }
        }
    ],
    _tag_total: 1,
    _tags: [{
        id: 0,
        name: '',
        _isJoinIn: 0
    }],
    _tagLastUpdatedAt: 0,
    _order_total: 1,
    _orders: [{
        id: 0,
        resource_id: 0,
        resource_price: 0,
        resource_type: 1,
        resource_created_by: 0,
        created_at: 0,
        updated_at: 0,
        deleted_at: 0,
        created_by: "1",
        updated_by: "1",
        orderno: "",
        paid_at: 0,
        cancel_type: 1,
        canceled_at: 0,
        buyer_criticism: null,
        buyer_rate: null,
        _isYour: 0,
        _createdBy: {
            id: 0,
            member_level: 1,
            username: "",
            email: "",
            status: 100,
            created_at: 0,
            signup_message: "",
            updated_at: 0,
            nickname: null,
            avatar: "",
            weixin_exceptional_code: "",
            weixin_exceptional_url: "",
            alipay_exceptional_code: "",
            alipay_exceptional_url: "",
            mobile: null,
            qq: "",
            weixin: null,
            _created_at: "2022-12-25 11:57:54"
        }
    }],
    _orderLastUpdatedAt: 0
}
const Resource = {
    toView(item) {
        // _this.$router.push({
        //   path: '/pages/resource/article/View',
        //   query: {
        //     id
        //   }
        // })
        uni.navigateTo({
            url: '/pages/resource/' + item._enType + '/View?id=' + item.id + '&updated_at=' + item.updated_at
        })
    },
    getIdCacheKey(id) {
        return "Resource:" + id;
    },
    getInitData() {
        return resource;
    },
    getCacheById(id) {
        let _this = this;
        let cacheModel =  Cache.get(_this.getIdCacheKey(id));
        if (!cacheModel) return null;
        cacheModel = _this.tagIsJoinInCheck(cacheModel);
        return cacheModel;
    },
    deleteCacheById(id) {
        let _this = this;
        Cache.delete(_this.getIdCacheKey(id));
    },
    setCacheById(id, cacheModel) {
        let _this = this;
        Cache.set(_this.getIdCacheKey(id), cacheModel);
    },
    async getOrSetCacheById(id, updated_at = 0) {
        let _this = this;
        let cacheModel = _this.getCacheById(id);
        // 是否需要更新
        let bool = !cacheModel || updated_at > cacheModel.updated_at;
        if (bool) {
            let res = await Api.post('/resource/default/get-by-id', {id, updated_at});
            if (res.data.type === 3 && res.data._videos[0]._userFile.m3u8_job_status !== 100) {
                cacheModel = res.data;
                return cacheModel;
            }
            _this.resetCacheForId(id, res.data);
            cacheModel = _this.getCacheById(id);
        }
        return cacheModel;
    },
    resetCacheForId(id, cacheModel) {
        let _this = this;
        _this.deleteCacheById(id);
        _this.setCacheById(id, cacheModel);
    },
    tagIsJoinInCheck(cacheModel) {
        // 是否是已加入的圈子
        if (cacheModel) {
            cacheModel._tags.forEach((v, k) => {
                cacheModel._tags[k]._isJoinIn = 0;
                if (Store.getters['Auth/getUserInfo']._tagIds.includes(v.id)) {
                    cacheModel._tags[k]._isJoinIn = 1;
                }
            })
        }
        return cacheModel;
    },
    toggleFav(item) {
        let _this = this;
        if (item._isFav > 0) {
            uni.showModal({
                title: '提示',
                content: '确定取消收藏吗？',
                success: function (res) {
                    if (res.confirm) {
                        Api.post('/resource/default/un-collect', {
                            id: item.id
                        }).then(respData => {
                            if (respData.status === 200) {
                                item._isFav = 0;
                                _this.resetCacheForId(item.id, item);
                                uni.$u.toast("取消收藏成功");
                            } else {
                                uni.$u.toast("取消收藏失败");
                            }
                        });
                    }
                }
            });
        } else {
            uni.showModal({
                title: '提示',
                content: '确定收藏吗？',
                success: function (res) {
                    if (res.confirm) {
                        Api.post('/resource/default/collect', {
                            id: item.id
                        }).then(respData => {
                            if (respData.status === 200) {
                                item._isFav = 1;
                                _this.resetCacheForId(item.id, item);
                                uni.$u.toast("收藏成功");
                            } else {
                                uni.$u.toast("收藏失败");
                            }
                        });
                    }
                }
            });
        }
    },
    del(item) {
        let _this = this;
        uni.showModal({
            title: '提示',
            content: '确定删除['+item.title+']吗？',
            success: function (res) {
                if (res.confirm) {
                    Api.post('/resource/default/del', {
                        id: item.id
                    }).then(respData => {
                        if (respData.status === 200) {
                        _this.deleteCacheById(item.id);
                            uni.navigateTo({
                                url: '/pages/resource/' + item._enType + '/List'
                            })
                        } else {
                            uni.$u.toast("删除失败");
                        }
                    });
                }
            }
        });
    }
}
export default Resource
